plotting dates
packages, functions, structures inside a .py file
In [1]:
    
# relevant packages for time
# from the standard library:
import time
import datetime
# external packages
import pytz
import dateutil
    
In [43]:
    
# time is based on the c time library
# Return the current time in seconds since the Epoch.
time.time()
    
    Out[43]:
In [42]:
    
?time
    
In [14]:
    
# epoch in greenwich:
time.gmtime(0)
    
    Out[14]:
In [13]:
    
# current time in greenwich
time.gmtime()
    
    Out[13]:
In [72]:
    
# object oriented date/time
import datetime
birthday = datetime.datetime(1976, 2, 23)
class Person:
    def __init__(self, name):
        self.name = name
    @staticmethod
    def born():
        person = Person(name=None)
        return person
Person.born()
    
    Out[72]:
In [45]:
    
now = datetime.datetime.now()
now
    
    Out[45]:
In [48]:
    
# common format
now.isoformat()
    
    Out[48]:
In [50]:
    
# date notation
now + datetime.timedelta(days=10)
    
    Out[50]:
In [53]:
    
import pytz
    
In [ ]:
    
    
In [74]:
    
# current timezone
tz = pytz.timezone('Europe/Amsterdam')
tz
    
    Out[74]:
In [75]:
    
# local time
tz.localize(now)
    
    Out[75]:
In [79]:
    
import dateutil.parser
    
In [85]:
    
for date in ['23/02/1976', '2000-01-02']:
    print(dateutil.parser.parse(date))
    
    
In [86]:
    
datetime.datetime.strptime('03/02/1976', '%d/%m/%Y')
    
    Out[86]:
In [88]:
    
import dateutil.rrule
    
In [91]:
    
    
    Out[91]:
In [92]:
    
import pandas
    
In [102]:
    
values = [1,2,3]
times = [datetime.datetime(2000,1,1), datetime.datetime(2000,1,2), datetime.datetime(2000,1,3)]
ts = pandas.TimeSeries(values, index=times)
    
In [103]:
    
import matplotlib.pyplot as plt
    
In [111]:
    
fig, ax = plt.subplots()
ax.plot(times, values)
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d-%m'))
ax.xaxis.set_major_locator(matplotlib.dates.DayLocator())
    
    
In [ ]: